using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;
using System.Collections.Generic;

public partial class Admin_BlogrollLinks : SFPage
{
    busBlogrollLinks insBusBlogrollLinks = new busBlogrollLinks();
    entBlogrollLinks insEntBlogrollLinks = new entBlogrollLinks();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgBlogrollLinks_NeedsDataSource()
    {
        DataTable insDataTableBlogrollLinks = insBusBlogrollLinks.SelectBlogrollLinks();
        if (insBusBlogrollLinks.HasErrors)
        {
            MessageBoxShow(insBusBlogrollLinks);
        }
        sfgBlogrollLinks.SetData(insDataTableBlogrollLinks);
        return insBusBlogrollLinks.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgBlogrollLinks_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntBlogrollLinks.BlogrollLinkId = Convert.ToInt32(e.CommandArgument);
            insBusBlogrollLinks.DeleteBlogrollLinksByBlogrollLinkId(insEntBlogrollLinks);
            if (insBusBlogrollLinks.HasErrors)
            {
                MessageBoxShow(insBusBlogrollLinks);
                return;
            }
            sfgBlogrollLinks.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstBlogrollLinks.LoadDataListData();

    }
    protected bool lstBlogrollLinks_NeedsDataSource()
    {
        insEntBlogrollLinks.BlogrollLinkId = this.Id;
        insBusBlogrollLinks.SelectBlogrollLinksByBlogrollLinkId(insEntBlogrollLinks);
        if (insBusBlogrollLinks.HasErrors)
        {
            MessageBoxShow(insBusBlogrollLinks);
            return true;
        }
        txtBlogrollLinkDescriptionMain.Text = insEntBlogrollLinks.BlogrollLinkDescription;

        entBlogrollLinkContents insEntBlogrollLinkContents = new entBlogrollLinkContents();
        insEntBlogrollLinkContents.BlogrollLinkId = insEntBlogrollLinks.BlogrollLinkId;
        busBlogrollLinkContents insBusBlogrollLinkContents = new busBlogrollLinkContents();
        DataTable insDataTableBlogrollLinkContents = insBusBlogrollLinkContents.SelectBlogrollLinkContentsByBlogrollLinkId(insEntBlogrollLinkContents);
        if (insBusBlogrollLinkContents.HasErrors)
        {
            MessageBoxShow(insBusBlogrollLinkContents);
            return true;
        }
        lstBlogrollLinks.SetData(insDataTableBlogrollLinkContents);
        return false;
    }


    List<entBlogrollLinkContents> insListBlogrollLinkContentsInsert = new List<entBlogrollLinkContents>();
    List<entBlogrollLinkContents> insListBlogrollLinkContentsUpdate = new List<entBlogrollLinkContents>();
    List<entBlogrollLinkContents> insListBlogrollLinkContentsDelete = new List<entBlogrollLinkContents>();
    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntBlogrollLinks.BlogrollLinkDescription = txtBlogrollLinkDescriptionMain.Text;

        foreach (DataListItem dli in lstBlogrollLinks.Items)
        {
            entBlogrollLinkContents insEntBlogrollLinkContents = new entBlogrollLinkContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntBlogrollLinkContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntBlogrollLinkContents.BlogrollLink = ((SFTextBox)dli.FindControl("txtBlogrollLink")).Text;
                if (!WebControls.Validation.ValidateWebSite(((SFTextBox)dli.FindControl("txtBlogrollLink")).Text))
                {
                    MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { ((SFLabel)dli.FindControl("lblBlogrollLink")).Text }));
                    return;
                }
                insEntBlogrollLinkContents.BlogrollLinkDescription = ((SFTextBox)dli.FindControl("txtBlogrollLinkDescription")).Text;
                insEntBlogrollLinkContents.BlogrollLinkName = ((SFTextBox)dli.FindControl("txtBlogrollLinkName")).Text;
                insEntBlogrollLinkContents.BlogrollLinkRss = ((SFTextBox)dli.FindControl("txtBlogrollLinkRss")).Text;
                if (lstBlogrollLinks.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntBlogrollLinkContents.BlogrollLinkContentId = Convert.ToInt32(lstBlogrollLinks.DataKeys[dli.ItemIndex]);
                    insListBlogrollLinkContentsUpdate.Add(insEntBlogrollLinkContents);
                }
                else
                {
                    insListBlogrollLinkContentsInsert.Add(insEntBlogrollLinkContents);
                }
            }
            else
            {
                if (lstBlogrollLinks.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntBlogrollLinkContents.BlogrollLinkContentId = Convert.ToInt32(lstBlogrollLinks.DataKeys[dli.ItemIndex]);
                    insListBlogrollLinkContentsDelete.Add(insEntBlogrollLinkContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusBlogrollLinks.InsertBlogrollLinks(insEntBlogrollLinks, insListBlogrollLinkContentsInsert, insListBlogrollLinkContentsUpdate, insListBlogrollLinkContentsDelete);
        if (insBusBlogrollLinks.HasErrors)
        {
            MessageBoxShow(insBusBlogrollLinks);
            return;
        }
        base.SaveNew();
        sfgBlogrollLinks.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntBlogrollLinks.BlogrollLinkId = this.Id;
        insBusBlogrollLinks.UpdateBlogrollLinksByBlogrollLinkId(insEntBlogrollLinks, insListBlogrollLinkContentsInsert, insListBlogrollLinkContentsUpdate, insListBlogrollLinkContentsDelete);
        if (insBusBlogrollLinks.HasErrors)
        {
            MessageBoxShow(insBusBlogrollLinks);
            return;
        }
        base.SaveExisting();
        sfgBlogrollLinks.LoadGridData();
    }

    protected void lstBlogrollLinks_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["BlogrollLinkContentId"] != DBNull.Value);
                e.Item.FindControl("pnlBlogrollLinkContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }

    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlBlogrollLinkContents").Visible = ((SFCheckBox)sender).Checked;
    }
}
